package com.example.eventbus;

import android.content.Intent;
import android.os.Bundle;
import android.text.style.BulletSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_message;
    private Button bt_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册事件
        EventBus.getDefault().unregister(MainActivity.this);
    }

    private void initView() {
        tv_message = (TextView) findViewById(R.id.tv_message);
        bt_message = (Button) findViewById(R.id.bt_message);
        bt_message.setOnClickListener(this);
        //注册事件
        EventBus.getDefault().register(MainActivity.this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainEvent(MessageEvent messageEvent){
        tv_message.setText(messageEvent.getString());
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_message:
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
                break;
        }
    }
}
